const app = getApp(),
    api = getApp().api;
var is_loading = !1,
    is_no_more = !1,
    p = 1;
Page({
    data: {
        state: false,
        show: 9,
        touchS: [0, 0],
        touchE: [0, 0],
        maskhide: false,
        order_status: 9,
        user: {},
        order_list: [],
        latitude: '',
        longitude: '',
        count: [],
        hide: 1,
        qrcode: ''
    },
    onLoad: function(t) {
        getApp().page.onLoad(this, t);
        var e = this;
        is_loading = is_no_more = !1, p = 1;
        this.getLocation();
    },
    // 页面数据
    loadData: function() {
        let o = this;
        console.log(o.data);
        is_loading || is_no_more || (is_loading = !0, getApp().request({
            url: getApp().api.user.get_delivery_list,
            data: {
                order_status: o.data.order_status,
                page: p,
                nowlatitude: o.data.latitude,
                nowlongitude: o.data.longitude
            },
            success: function(t) {
                if (0 == t.code) {
                    o.setData({
                        user: t.data.user,
                        state: t.data.user.delivery_office == 0 ? true : false,
                        count: t.data.count
                    }), 0 == t.data.list.length && (is_no_more = !0);
                    if (p > 1) {
                        o.data.order_list.push(...t.data.list);
                        o.setData({
                            order_list: o.data.order_list
                        });
                        console.log(o.data.order_list);
                    } else {
                        o.setData({
                            order_list: t.data.list
                        });
                    }
                }
                p++;
            },
            complete: function() {
                is_loading = !1;
            }
        }));
    },
    // 定位获取经纬度
    getLocation: function() {
        let o = this;
        wx.getLocation({
            type: 'wgs84',
            success: (res) => {
                console.log(res);
                var latitude = res.latitude
                var longitude = res.longitude
                o.setData({
                    latitude: latitude,
                    longitude: longitude
                }), o.loadData();
            }

        })
    },
    // 
    maskclick() {
        var mask1 = !this.data.maskhide
        this.setData({
            maskhide: mask1
        })
    },
    // 页面头部切换
    tabclick(e) {
        this.setData({
            show: e.currentTarget.dataset.index,
            order_status: e.currentTarget.dataset.index
        })
        p = 1;
        is_no_more = !1;
        this.loadData();
    },
    // 跳转页面
    goUrl: function(e) {
        var gopage = e.currentTarget.dataset.url;
        if (!gopage) {
            return;
        }
        getApp().core.navigateTo({
            url: gopage
        });
    },
    // 下线休息
    down_line: function() {
        var a = {
            delivery_office: 1
        };
        let e = this;
        getApp().request({
            url: getApp().api.user.delivery_user_status,
            data: a,
            success: function(t) {
                0 == t.code && (e.setData({
                    state: false,
                    maskhide: false
                }), getApp().core.showModal({
                    title: "退出大厅",
                    content: "是否退出配送员任务大厅",
                    confirmText: "退出",
                    success: function(e) {
                        e.confirm && getApp().core.switchTab({
                            url: '/pages/user/user'
                        });
                    }
                }));
            },
            complete: function() {
                getApp().core.hideLoading();
            }
        });
    },
    // 上线接单
    come() {
        var a = {
            delivery_office: 0
        };
        let e = this;
        getApp().request({
            url: getApp().api.user.delivery_user_status,
            data: a,
            success: function(t) {
                0 == t.code && (e.setData({
                    state: true,
                    maskhide: false
                }));
            },
            complete: function() {
                getApp().core.hideLoading();
            }
        });
    },
    // 进行拨打电话
    telShop: function(e) {
        console.log(e);
        var phone = e.currentTarget.dataset.phone;
        if (phone) {
            wx.makePhoneCall({
                phoneNumber: phone
            })
        } else {
            getApp().core.showModal({
                title: "拨打失败",
                content: "商家未绑定手机号",
                showCancel: !1
            });
        }
        console.log(e);
    },
    // 在线抢单
    grabOrder: function(e) {
        var order_id = e.currentTarget.dataset.id,
            order_type = e.currentTarget.dataset.order_type,
            is_can = e.currentTarget.dataset.is_can;
        if (is_can == 0) {
            return
        }
        if (!order_id || !order_type) {
            getApp().core.showModal({
                title: "抢单失败",
                content: "抢单信息不完整",
                showCancel: !1
            });
        }
        var a = {
            order_id: order_id,
            order_type: order_type,
            time: parseInt(new Date().getTime() / 1000)
        };
        let f = this;
        getApp().request({
            url: getApp().api.user.delivery_grab_order,
            data: a,
            method: "post",
            success: function(t) {
                if (t.code == 0) {
                    getApp().core.showModal({
                        title: "抢单成功",
                        content: "恭喜，快去配送吧~",
                        showCancel: !1,
                        success: function(a) {
                            a.confirm && f.loadSet();
                        }
                    });
                } else {
                    getApp().core.showModal({
                        title: "抢单失败",
                        content: t.errorMsg,
                        showCancel: !1,
                        success: function(ea) {
                            a.confirm && f.loadSet();
                        }
                    });
                }

            },
            complete: function() {
                getApp().core.hideLoading();
            }
        });
    },
    // 到达店铺进行取货
    takeGoods: function(y) {
        var th = this;
        getApp().request({
            url: getApp().api.order.peisong,
            data: {
                oid: y.target.dataset.oid,
                status: y.target.dataset.status,
                delivery_type: y.target.dataset.order_type,
            },
            success: function(t) {
                if (t.code == 0) {

                    getApp().core.showModal({
                        title: "取货成功",
                        content: "恭喜，快去配送吧~",
                        showCancel: !1,
                        success: function(a) {
                            a.confirm && th.loadSet();
                        }
                    });
                } else {
                    getApp().core.showModal({
                        title: "取货失败",
                        content: t.errorMsg,
                        showCancel: !1,
                        success: function(a) {
                            a.confirm && th.loadSet();
                        }
                    });
                }
            }
        })
    },
    // 到达收货地点，获取二维码用户扫描确认收货，订单完成
    orderQrcode: function(a) {
        var o = this,
            order_id = a.target.dataset.oid,
            delivery_type = a.target.dataset.order_type;

            getApp().core.showModal({
                title: "提示",
                content: "确认收货方式",
                cancelText: "二维码",
                confirmText: "确认到家",
                success: function(t) {
                    t.cancel && (
                        wx.showLoading({
                            title: "正在加载",
                            mask: !0
                        }), getApp().request({
                            url: getApp().api.user.get_delivery_qrcode,
                            data: {
                                order_id: order_id,
                                delivery_type: delivery_type
                            },
                            success: function(t) {
                                0 == t.code ? o.setData({
                                    hide: 0,
                                    qrcode: t.data.url
                                }) : wx.showModal({
                                    title: "提示",
                                    content: t.msg
                                });
                            },
                            complete: function() {
                                wx.hideLoading();
                            }
                        })
                    );
                    t.confirm && (getApp().core.showLoading({
                        title: "操作中"
                    }), getApp().request({
                        url: getApp().api.order.delivery_order_confirm,
                        data: {
                            order_id: order_id,
                            delivery_type: delivery_type,
                            type: 1
                        },
                        success: function(t) {
                            getApp().core.hideLoading(), getApp().core.showToast({
                                title: t.msg
                            }),o.onLoad();
                        }
                    }));
                }
            });
    },
    hide: function(t) {
        this.setData({
            hide: 1
        });
    },
    // 刷新页面数据
    loadSet: function() {
        p = 1;
        is_no_more = !1;
        this.loadData();
    },
    touchStart: function(e) {
        let sx = e.touches[0].pageX
        let sy = e.touches[0].pageY
        this.data.touchS = [sx, sy]
    },
    touchEnd: function(e) {
        let sx = e.changedTouches[0].pageX;
        let sy = e.changedTouches[0].pageY;
        this.data.touchE = [sx, sy]
        let start = this.data.touchS
        let end = this.data.touchE
        if (start[0] < end[0] - 50 && Math.abs(end[1] - start[1]) < 50) {
            this.setData({
                maskhide: true
            })
        } else if (start[0] > end[0] + 50 && Math.abs(end[1] - start[1]) < 50) {
            this.setData({
                maskhide: false
            })
        }
    },
    onReady: function() {
        getApp().page.onReady(this);
    },
    onShow: function() {
        getApp().page.onShow(this);
    },
    onHide: function() {
        getApp().page.onHide(this);
    },
    onUnload: function() {
        getApp().page.onUnload(this);
    },
    //下拉刷新
    onPullDownRefresh: function() {
        getApp().page.onPullDownRefresh(this);
        this.loadSet();
        console.log('下拉刷新');
    },
    // 上拉加载
    onReachBottom: function() {
        console.log('上拉加载');
        is_no_more || this.loadData();
    }

})
